package com.tedu.boot.controller;

import com.tedu.boot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class LandAccounts {
    private static File[] users;
    private User user;
    static{
        File file=new File("./users");
        FileFilter filter=new FileFilter() {
            public boolean accept(File dir) {
                return !dir.isDirectory();
            }
        };
        users=file.listFiles(filter);

    }

@RequestMapping("/landaccounts")
public void land(HttpServletRequest request, HttpServletResponse response){
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    InputStream is;
    ObjectInputStream ois;
    try {
        for (File f:users){
            is=new FileInputStream(f);
            ois=new ObjectInputStream(is);
            User user=(User) ois.readObject();
                if(password.equals(user.getPassword())||username.equals(user.getUsername())){
                    this.user=user;
                    response.sendRedirect("/land.html");
                    return;
                }
            }
        response.sendRedirect("/passwordError.html");
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}
}
